
/***************************************************************************
 *   jell - library                                                        *
 *                                                                         *
 *   Copyright (C) 2008 by kuerant                                         *
 *                         <kuerant@gmail.com>                             *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This library is distributed in the hope that it will be usefull, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Library General Public License for more details.                      *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston,                *
 *   MA 02111-1307, USA                                                    *
 ***************************************************************************/

#include "jell/exception.hpp"

namespace jell {

    std::ostream&
    operator<<(std::ostream& out, const JellException& ex)
    {
        //out <<"JellException: " << "[" << ex.code() << "] " << ex.what();
        out <<"JellException: ";

        if (ex.line()) {
            //out <<"\n\tat "<< ex.func() <<" ("<< ex.file() <<':'<< ex.line() <<')';
            out <<"at "<< ex.func() <<" ("<< ex.file() <<':'<< ex.line() <<"): "
                << "[" << ex.code() << "] " << ex.what();
        }
        else {
            out << "[" << ex.code() << "] " << ex.what();
        }

        for (JellException *prev = ex.prev(); prev; prev = prev->prev()) {
            if (prev->line()) {
                out <<"\n           at "<< (prev->func() ? prev->func() : "-") <<" ("
                    << (prev->file() ? prev->file() : "-") <<':'<< prev->line() <<"): "
                    << "[" << prev->code() << "] "
                    << prev->what();
            }
            else {
                out <<"\n           " << "[" << prev->code() << "] " << prev->what();
            }
        }

        return out;
    }


}   // namespace jell
